package com.taxshare.api.service;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONUtil;
import com.taxshare.api.view.dto.*;

import com.taxshare.response.beans.Response;
import com.taxshare.tools.util.Base64Util;
import com.taxshare.tools.util.StringUtil;
import com.taxshare.api.util.SignStringUtil;
import com.taxshare.api.view.vo.PurchaserApiRespVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


/**
 * @author YANG
 * @date 2021/4/6
 * @Description:进项发票查验
 */
@Service
@Slf4j
public class PurchaserInvoiceService {

    @Value("${purchaser.appid}")
    private String appid;
    @Value("${purchaser.appsecret}")
    private String appsecret;
    @Value("${purchaser.jtnsrsbh}")
    private String jtnsrsbh;
    @Value("${purchaser.url}")
    private String url;
    @Value("${purchaser.checkCode}")
    private String checkCode;
    @Value("${purchaser.ocrMoreCode}")
    private String ocrCode;

    @Autowired
    RestTemplate customRest;

    /**
     * 进项发票查验
     *
     * @param checkInvoiceDTO
     * @return
     */
    public Response<PurchaserApiRespVO> invoiceCheck(CheckInvoiceDTO checkInvoiceDTO) {
        PurchaserApiDTO purchaserApiDTO = new PurchaserApiDTO();
        CheckInvoiceContent checkInvoiceContent = new CheckInvoiceContent();
        checkInvoiceContent.setFplx(checkInvoiceDTO.getInvoiceType());
        checkInvoiceContent.setFphm(checkInvoiceDTO.getInvoiceNo());
        checkInvoiceContent.setFpdm(checkInvoiceDTO.getInvoiceCode());
        checkInvoiceContent.setKprq(checkInvoiceDTO.getInvoiceDate());
        checkInvoiceContent.setFpje(checkInvoiceDTO.getAmountWithOutTax());
        checkInvoiceContent.setJym(checkInvoiceDTO.getCheckCode());
        String checkCodeStr = checkInvoiceDTO.getCheckCode();
        String invoiceType = checkInvoiceDTO.getInvoiceType();
        String[] invoiceTypes;
        invoiceTypes = new String[]{"04", "10", "11", "14"};
        if (ArrayUtil.contains(invoiceTypes, invoiceType) && !StringUtil.isEmpty(checkCodeStr)) {
            checkInvoiceContent.setJym(checkCodeStr.substring(checkCodeStr.length() - 6));
        }
        String content = Base64Util.encode(checkInvoiceContent.toString());
        purchaserApiDTO.setContent(content);
        purchaserApiDTO.setServiceid(checkCode);
        ResponseEntity<PurchaserApiRespVO> response = httpInfo(purchaserApiDTO);
        return Response.success(response.getBody());
    }

    /**
     * 进项发票识别
     *
     * @param imageUploadInvoiceDTO
     * @return
     */
    public Response invoiceImageRecognition(ImageUploadInvoiceDTO imageUploadInvoiceDTO) {
        PurchaserApiDTO purchaserApiDTO = new PurchaserApiDTO();
        purchaserApiDTO.setServiceid(ocrCode);
        OcrImageContent ocrImageContent = new OcrImageContent();
        ocrImageContent.setImg(imageUploadInvoiceDTO.getImageBase64Str());
        String content = Base64Util.encode(JSONUtil.toJsonStr(ocrImageContent));
        purchaserApiDTO.setContent(content);
        ResponseEntity<PurchaserApiRespVO> response = httpInfo(purchaserApiDTO);
        return Response.success(response.getBody());
    }

    /**
     * http 请求
     *
     * @param purchaserApiDTO
     * @return
     */
    public ResponseEntity<PurchaserApiRespVO> httpInfo(PurchaserApiDTO purchaserApiDTO) {
        purchaserApiDTO.setAppid(appid);
        purchaserApiDTO.setJtnsrsbh(jtnsrsbh);
        String signR = SignStringUtil.sign(appsecret, purchaserApiDTO.toString());
        purchaserApiDTO.setSignature(signR);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity request = new HttpEntity<>(purchaserApiDTO, headers);
        ResponseEntity<PurchaserApiRespVO> response;
        response = customRest.postForEntity(url, request, PurchaserApiRespVO.class);
        //log.info("remote invoke response :{}", response);
        //log.info("remote invoke response :{}", response.getBody());
        return response;


    }

}
